package com.bytedance.improvingroad.behavior

import android.animation.ArgbEvaluator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.widget.TextView
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.bytedance.improvingroad.widget.DefineDragView

/**
 * Created by chenxiong
 * date 1/26/22
 */
class ChangeColorBehavior @JvmOverloads constructor(
    context: Context,
    attr: AttributeSet? = null,
    style: Int = 0
) : CoordinatorLayout.Behavior<TextView>() {

    private val mArgbEvaluator = ArgbEvaluator()

    override fun layoutDependsOn(
        parent: CoordinatorLayout,
        child: TextView,
        dependency: View
    ): Boolean {
        return dependency is DefineDragView
    }

    @SuppressLint("ResourceAsColor")
    override fun onDependentViewChanged(
        parent: CoordinatorLayout,
        child: TextView,
        dependency: View
    ): Boolean {
        val color = mArgbEvaluator.evaluate(dependency.y / parent.height, Color.WHITE, Color.BLACK) as Int
        child.setTextColor(color)
        return true
    }
}